package medM;

import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class MedMessageFindServlet
 */
@WebServlet("/MedMessageFindServlet")
public class MedMessageFindServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MedMessageFindServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK"); 
		String s=new String(request.getParameter("find").getBytes("ISO-8859-1"),"GBK");
		int i;
		String find;
		String[] ss=s.split(",");
		if(ss.length==1){
			i=1;
			find=request.getParameter("find");
		}else{
			i=Integer.parseInt(ss[0]);
			find=ss[1];
		}
		int j=0;
		DBTools db = new DBTools();
		db.open();
		String sql="SELECT * FROM medInfo WHERE normalName LIKE '%"+find+"%' OR medName LIKE '%"+find+"%' OR englishName LIKE '%"+find+"%';";
		ResultSet rs=db.select(sql);
		ArrayList al=new ArrayList();
		try {
			while(rs.next()){
				MedMessageBean mmb=new MedMessageBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9),rs.getString(10),rs.getString(11),rs.getString(12),rs.getString(13),rs.getString(14),rs.getString(15),rs.getInt(16),rs.getString(17),rs.getString(18),rs.getString(19),rs.getString(20),rs.getString(21),rs.getString(22),rs.getString(23),rs.getString(24),rs.getString(25));
				al.add(mmb);
			}
			if(al.size()%5!=0){
				j=al.size()/5+1;
				if(i<1){
					i=al.size()/5+1;
				}
				if(i>al.size()/5+1){
					i=1;
				}
			}else{
				j=al.size()/5;
				
				if(i<1){
					i=al.size()/5;
				}
				if(i>al.size()/5){
					i=1;
				}
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String sql2="select * from medInfo WHERE normalName LIKE '%"+find+"%' OR medName LIKE '%"+find+"%' OR englishName LIKE '%"+find+"%' limit "+(i-1)*5+","+"5";
		ResultSet rss=db.select(sql2);
		ArrayList al1=new ArrayList();
		try {
			while(rss.next()){
				MedMessageBean mmb=new MedMessageBean(rss.getInt(1),rss.getString(2),rss.getString(3),rss.getString(4),rss.getString(5),rss.getString(6),rss.getString(7),rss.getString(8),rss.getString(9),rss.getString(10),rss.getString(11),rss.getString(12),rss.getString(13),rss.getString(14),rss.getString(15),rss.getInt(16),rss.getString(17),rss.getString(18),rss.getString(19),rss.getString(20),rss.getString(21),rss.getString(22),rss.getString(23),rss.getString(24),rss.getString(25));
				al1.add(mmb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		request.setAttribute("find", find);
		request.setAttribute("al", al1);
		request.setAttribute("i", i);
		request.setAttribute("j", j);
		request.getRequestDispatcher("/MedMessageFind.jsp").forward(request, response);
	}

}
